package com.robot.member.service;

import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import com.robot.member.dao.CartDetailsDao;
import com.robot.member.pojo.CartDetails;

/**
 * details服务层
 *
 * @author wuyunbin
 */
@Service
public class CartDetailsService {

    @Autowired
    private CartDetailsDao cartDetailsDao;

    @Autowired
    private IdWorker idWorker;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<CartDetails> findAll() {
        return cartDetailsDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<CartDetails> findSearch(Map whereMap, int page, int size) {
        Specification<CartDetails> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return cartDetailsDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<CartDetails> findSearch(Map whereMap) {
        Specification<CartDetails> specification = createSpecification(whereMap);
        return cartDetailsDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public CartDetails findById(String id) {
        return cartDetailsDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param details
     */
    public void add(CartDetails details) {
        cartDetailsDao.save(details);
    }

    /**
     * 修改
     *
     * @param details
     */
    public void update(CartDetails details) {
        cartDetailsDao.save(details);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        cartDetailsDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<CartDetails> createSpecification(Map searchMap) {
        return new Specification<CartDetails>() {
            @Override
            public Predicate toPredicate(Root<CartDetails> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 购物车id
                String cartId = "cartId";
                if (searchMap.get(cartId) != null && !"".equals(searchMap.get(cartId))) {
                    predicateList.add(cb.like(root.get(cartId).as(String.class), "%" + (String) searchMap.get(cartId) + "%"));
                }
                // 商品id
                String goodsId = "goodsId";
                if (searchMap.get(goodsId) != null && !"".equals(searchMap.get(goodsId))) {
                    predicateList.add(cb.like(root.get(goodsId).as(String.class), "%" + (String) searchMap.get(goodsId) + "%"));
                }
                // 商品名称
                String goodsName = "goodsName";
                if (searchMap.get(goodsName) != null && !"".equals(searchMap.get(goodsName))) {
                    predicateList.add(cb.like(root.get(goodsName).as(String.class), "%" + (String) searchMap.get(goodsName) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过cartId和goodsId查找CartDetails
     *
     * @param cartId
     * @param goodsId
     * @return
     */
    public CartDetails findByCartIdAndGoodsIdAndDeletedAtIsNull(String cartId, String goodsId) {
        return cartDetailsDao.findByCartIdAndGoodsIdAndDeletedAtIsNull(cartId, goodsId);
    }


    /**
     * 通过cartId查找CartDetails
     *
     * @param cartId
     * @return
     */
    public List<CartDetails> findAllByCartIdAndDeletedAtIsNull(String cartId) {
        return cartDetailsDao.findAllByCartIdAndDeletedAtIsNull(cartId);
    }

    /**
     * 通过cartId删除CartDetails
     *
     * @param cartId
     * @return
     */
    public void deleteAllByCartId(String cartId) {
        cartDetailsDao.deleteAllByCartId(cartId);
    }

    /**
     * 通过cartId和goodsId删除CartDetails
     *
     * @param cartId
     * @param goodsId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByCartIdAndGoodsId(String cartId, String goodsId) {
        cartDetailsDao.deleteByCartIdAndGoodsId(cartId, goodsId);
    }
}
